#pragma once
#include<assert.h>
#include"reverse_iterator.h"
namespace lh
{
	//结点
	template<class T>
	struct list_node
	{
		T _date;
		list_node<T>* _next;
		list_node<T>* _prev;

		list_node(const T& x=T())
			:_date(x)
			,_next(nullptr)
			,_prev(nullptr)
		{}
	};
	//迭代器
	template<class T,class Ref,class Ptr>
	struct __list_iterator
	{
		typedef list_node<T> Node;
		typedef __list_iterator<T, Ref, Ptr> iterator;

	/*	typedef bidirectional_iterator_tag iterator_category;
		typedef T value_type;
		typedef Ptr pointer;
		typedef Ref reference;
		typedef ptrdiff_t difference_type;*/

		Node* _node;

		__list_iterator( Node* node)
			:_node(node)
		{}

		//传引用，减少拷贝构造
		bool operator!=(const iterator& it)const
		{
			return _node != it._node;
		}

		bool operator==(const iterator& it)const
		{
			return _node == it._node;
		}

		//++it
		iterator& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		//it++
		iterator operator++(int)
		{
			iterator tmp(*this);
			_node = _node->_next;
			return tmp;
		}

		//--it
		iterator& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		//it--
		iterator operator--(int)
		{
			iterator tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

		//*操作	T& operator*()
		Ref operator*()
		{
			return _node->_date;
		}

		//->操作	T* operator->()
		Ptr operator->()
		{
			return &(operator*());
		}
	};


	//链表
	template<class T>
	class list
	{
		//默认为private
		typedef list_node<T> Node;
	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator< T, const T&, const T*> const_iterator;
		
		typedef Reverse_iterator<iterator, T&, T*> reverse_iterator;
		typedef const Reverse_iterator<iterator, T&, T*> const_reverse_iterator;


		//创建并初始化链表哨兵位
		void empty_list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		list()
		{
			empty_list();
		}

		template <class InputIterator>  
		list(InputIterator first, InputIterator last)
		{
			empty_list();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		void swap(list<T>& x)
		{
			std::swap(_head, x._head);
		}

		//lt2(lt1)
		list(const list<T>& lt)
		{
			empty_list();
			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}

		//lt2=lt1
		list& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}

		const_reverse_iterator rbegin() const
		{
			return reverse_iterator(end());
		}

		const_reverse_iterator rend() const
		{
			return reverse_iterator(begin());
		}

		//返回指针，没必要用传引用返回，指向的是一块区域
		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin()const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end()const
		{
			return const_iterator(_head);
		}


		iterator insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			//开空间再利用构造函数初始化
			Node* newnode =new Node(x);

			newnode->_next = cur;
			cur->_prev = newnode;
			prev->_next = newnode;
			newnode->_prev = prev;
			return iterator(newnode);
			//return --pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;

			delete cur;
			return iterator(next);
		}

		//尾插
		void push_back(const T& x)
		{
			insert(end(), x);
		}
		//头插
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		//尾删
		void pop_back()
		{
			erase(--end());
		}
		//头删
		void pop_front()
		{
			erase(begin());
		}

	private:
		Node* _head;
	};


	void test_list1()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << ' ';
			++it;
		}
		cout << endl;
		it = lt.begin();
		while (it!=lt.end())
		{
			*it *= 2;
			cout << *it << ' ';
			++it;
		}
		cout << endl;

	}

	void test_list2()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		lt.pop_back();
		lt.pop_back();
		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << ' ';
			++it;
		}
		cout << endl;
	}

	void test_list3()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		lt.pop_front();
		lt.pop_front();

		lt.push_front(10);
		lt.push_front(20);

		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << ' ';
			++it;
		}
		cout << endl;

	}


	void test_list4()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		list<int> lt2;
		lt2=lt;

		lt.pop_back();
		lt.pop_front();

		list<int>lt3(lt);

		list<int>::iterator it = lt2.begin();
		while (it != lt2.end())
		{
			cout << *it << ' ';
			++it;
		}
		cout << endl;
		list<int>::iterator it3 = lt3.begin();
		while (it3 != lt3.end())
		{
			cout << *it3 << ' ';
			++it3;
		}
		cout << endl;
	}
};